﻿using HaiSpider.Component.Http;
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace 易发车网.Untils
{
    public static class PotatoApi
    {
        private const string Url_SendMessage = "https://api.rct2008.com:8443/10280895:y1FAT3WWTTC3PgZq9clI4Dt3/sendTextMessage";
        private const string Url_SnedPhoto = "https://api.rct2008.com:8443/10280895:y1FAT3WWTTC3PgZq9clI4Dt3/sendphoto";

        public static async Task SendMessage(string content)
        {
            try
            {
                var client = HaiHttp.CreateDefaultHttpClient();
                var request = HaiHttp.CreateDefaultPostRequest(Url_SendMessage);
                request.Content = new StringContent(CreatePostData(content), Encoding.UTF8, "application/json");
                var x = CreatePostData(content);
                var re = await client.SendAsync(request);
                Console.WriteLine(await re.Content.ReadAsStringAsync());
            }
            catch
            {

            }
        }

        public static async Task SendPhoto(string src)
        {
            var client = HaiHttp.CreateDefaultHttpClient();
            var request = HaiHttp.CreateDefaultPostRequest(Url_SendMessage);
            request.Content = new  StringContent(CreatePostPhotoData(src),Encoding.UTF8, "multipart/form-data");
            var re = await client.SendAsync(request);
            Console.WriteLine(await re.Content.ReadAsStringAsync());
        }


        private static string CreatePostPhotoData(string src)
        {
            return $"{{\"chat_type\": 3,\"chat_id\": 12701581, \"photo\": \"{new FileStream(src,FileMode.Open)}\"}}";
        }

        private static string CreatePostData(string text)
        {
            return $"{{\"chat_type\": 3,\"chat_id\": 12701581,\"text\": \"{text}\",\"markdown\": true}}";
        }
    }
}
